namespace DotNetNuke.Services.FileSystem
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Exceptions;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Data;
    using System.Runtime.CompilerServices;
    using System.Web;

    /// -----------------------------------------------------------------------------
    /// Project	 : DotNetNuke
    /// Class	 : FolderController
    /// 
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// Business Class that provides access to the Database for the functions within the calling classes
    /// Instantiates the instance of the DataProvider and returns the object, if any
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// -----------------------------------------------------------------------------
    public class FolderController
    {
        public int AddFolder(int PortalID, string FolderPath)
        {
            int FolderId;
            IDataReader dr = DataProvider.Instance().GetFolder(PortalID, FolderPath);
            if (dr.Read())
            {
                FolderId = Convert.ToInt32(RuntimeHelpers.GetObjectValue(dr["FolderId"]));
                DataProvider.Instance().UpdateFolder(PortalID, FolderId, FolderPath);
            }
            else
            {
                FolderId = DataProvider.Instance().AddFolder(PortalID, FolderPath);
            }
            dr.Close();
            return FolderId;
        }

        public void DeleteFolder(int PortalID, string FolderPath)
        {
            DataProvider.Instance().DeleteFolder(PortalID, FolderPath);
        }

        public ArrayList GetFolder(int PortalID, int FolderID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetFolder(PortalID, FolderID), typeof(FolderInfo));
        }

        public FolderInfo GetFolder(int PortalID, string FolderPath)
        {
            return (FolderInfo) CBO.FillObject(DataProvider.Instance().GetFolder(PortalID, FolderPath), typeof(FolderInfo));
        }

        public ArrayList GetFoldersByPortal(int PortalID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetFoldersByPortal(PortalID), typeof(FolderInfo));
        }

        public string GetMappedDirectory(string VirtualDirectory)
        {
            if (DataCache.GetCache("DirMap:" + VirtualDirectory) == null)
            {
                this.SetMappedDirectory(VirtualDirectory);
            }
            return Convert.ToString(RuntimeHelpers.GetObjectValue(DataCache.GetCache("DirMap:" + VirtualDirectory)));
        }

        public void SetMappedDirectory(string VirtualDirectory)
        {
            try
            {
                string MappedDir = FileSystemUtils.AddTrailingSlash(HttpContext.Current.Server.MapPath(VirtualDirectory));
                DataCache.SetCache("DirMap:" + VirtualDirectory, MappedDir);
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                
            }
        }

        public void SetMappedDirectory(PortalInfo portalInfo, HttpContext context)
        {
            try
            {
                string VirtualDirectory = Globals.ApplicationPath + "/" + portalInfo.HomeDirectory + "/";
                this.SetMappedDirectory(VirtualDirectory, context);
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                
            }
        }

        public void SetMappedDirectory(string VirtualDirectory, HttpContext context)
        {
            try
            {
                string MappedDir = FileSystemUtils.AddTrailingSlash(context.Server.MapPath(VirtualDirectory));
                DataCache.SetCache("DirMap:" + VirtualDirectory, MappedDir);
            }
            catch (Exception exception1)
            {
                
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);
                
            }
        }

        public void UpdateFolder(FolderInfo objFolderInfo)
        {
            DataProvider.Instance().UpdateFolder(objFolderInfo.PortalID, objFolderInfo.FolderID, objFolderInfo.FolderPath);
        }
    }
}

